package org.jwflow.design.servlet;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.jwflow.design.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/design")
public class DesignServlet extends BaseServlet {

    public void index(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //装载map数据模型信息
        Map root = new HashMap();
        root.put("information", "欢迎光临！O(∩_∩)O哈哈~");

        Template template=cfg.getTemplate("design.html");
        //设置响应输出类型和编码格式
        //设置浏览器用utf8来解析返回的数据
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        //设置servlet用UTF-8转码，而不是用默认的ISO8859
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();//创建相应内容写出对象

        //将map数据模型与模板结合输出
        try {
            template.process(root, out);
        } catch (TemplateException e) {
            throw new ServletException("加载Freemarker模板失败！",e);
        }

    }

}
